﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Data
{
    public class Connect
    {
        SqlConnection conn = new SqlConnection();

        //Get Connection
        public Connect()
        {
            string connect = null;
            try
            {
                try
                {
                    connect = ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
                    conn = new SqlConnection(connect);
                    conn.Open();
                }
                catch
                {
                    connect = @"Data Source=.;Initial Catalog=ATMSystem;User ID=sa;Password=12345";
                    conn = new SqlConnection(connect);
                    conn.Open();
                }
            }
            catch { }
        }

        public static SqlConnection GetConnect()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ConnectionString);
            if (conn.State == ConnectionState.Open)
            {
                conn.Open();
                conn.Dispose();
            }
            return conn;
        }

        //Get all data
        public DataTable GetData(string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                cmd.Dispose();
            }
            catch
            {
                dt = null;
            }
            return dt;
        }

        //Get data by paramaster
        public DataTable GetData(string sql, string[] name, object[] value, int Npara)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                for (int i = 0; i < Npara; i++)
                {
                    cmd.Parameters.AddWithValue(name[i], value[i]);
                    da.Fill(dt);
                }
                cmd.Dispose();
            }
            catch
            {
                dt = null;
            }
            return dt;
        }

        //Nonquery to insert, update, delete
        public bool NonQuery(string sql, string[] name, object[] value, int Npara)
        {
            bool check = false;
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                for (int i = 0; i < Npara; i++)
                {
                    cmd.Parameters.AddWithValue(name[i], value[i]);
                }
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                check = true;
            }
            catch
            {
                check = false;
            }
            return check;
        }        
    }
}
